package com.coder4j.log.controller;

import com.coder4j.log.entity.StatisticsLogEntity;
import com.coder4j.log.service.IStatisticsLogService;
import com.coder4j.log.vo.StatisticsLogQuery;
import com.coder4j.log.vo.StatisticsLogVO;
import com.coder4j.log.vo.StatisticsLogEO;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 统计日志
 * @author keller
 * @date 2024-11-27 15:02:45
 */
@Slf4j
@RestController
@RequestMapping("/statisticsLog")
public class StatisticsLogController {
    @Resource
    private IStatisticsLogService service;

    @Resource
    private HttpServletResponse response;

    /**
     * 按ID查询统计日志
     * @param id StatisticsLog.id
     * @return 统计日志对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询统计日志",createName = "keller",createTime = "2024-11-27 15:02:45")
    public R<StatisticsLogVO> getById( Long id){

        return R.success(service.baseGetVOById(id));
    }

    /**
     * 保存统计日志
     * 有 id 字段，更新；否则新增
     * @param statisticsLog StatisticsLog对象
     * @return StatisticsLog对象
     */
    @PostMapping("/save")
    @ApiNote(name="保存统计日志",createName = "keller",createTime = "2024-11-27 15:02:45")
    public R save(@RequestBody StatisticsLogVO statisticsLog){

        if(ObjectUtils.hasEmpty(statisticsLog.getStatisticsType(),statisticsLog.getStatisticsDataStatus(),statisticsLog.getStatisticsData())){
            return R.paramError();
        }

        service.baseSaveVO(statisticsLog);
        return R.success();
    }

    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    @ApiNote(name="查询统计日志列表",createName = "keller",createTime = "2024-11-27 15:02:45")
    public R<BasePage<StatisticsLogVO>> getList(@RequestBody StatisticsLogQuery query){
        if(query == null){
            query = new StatisticsLogQuery();
        }
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 导出列表
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name="导出统计日志列表",createName = "keller",createTime = "2024-11-27 15:02:45")
    public void export(@RequestBody StatisticsLogQuery query){
        if(query == null){
            query = new StatisticsLogQuery();
        }
        List<StatisticsLogEntity> list = service.baseQueryList(query);
                List<StatisticsLogEO> eoList = new ArrayList<>();
                for (StatisticsLogEntity entity : list) {
                    StatisticsLogEO eo = new StatisticsLogEO();
                    BeanUtils.copyProperties(entity,eo);
                    eoList.add(eo);
                }
                EasyExcelUtils.exportClass(response,"StatisticsLogList",eoList);
    }


    /**
     * 删除统计日志
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id StatisticsLog.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除统计日志",createName = "keller",createTime = "2024-11-27 15:02:45")
    public R deleteById( Long id){
        service.baseDeleteById(id);
        return R.success();
    }
}
